package com.tools.web.interceptor.auth.jwt;

import com.auth0.jwt.algorithms.Algorithm;
import com.tools.common.object.Note;
import com.tools.middle.redis.client.JedisClusterPlus;
import com.tools.web.interceptor.auth.AuthSource;
import com.tools.web.interceptor.auth.AuthToken;
import com.tools.web.interceptor.auth.AuthUserService;
import com.tools.web.interceptor.auth._common.JedisClusterPlusSingleAuthenticationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

/**
 * 【JWT + JedisClusterPlus 集群客户端操作 Redis 集群】 实现的 “用户单在线记录” 的 AuthenticationManager 身份认证拦截器
 * */
@Note("【JWT + JedisClusterPlus 集群客户端操作 Redis 集群】 实现的 “用户单在线记录” 的 AuthenticationManager 身份认证拦截器")
public final class JWTJedisClusterPlusSingleAuthenticationManager extends JedisClusterPlusSingleAuthenticationManager {

    @Note("JWT 实现的 AuthToken 用于加密解密的工具")
    private final Algorithm authTokenAlgorithm;

    @Note("日志输出对象")
    private static final Logger LOGGER = LoggerFactory.getLogger(JWTJedisClusterPlusSingleAuthenticationManager.class);

    public JWTJedisClusterPlusSingleAuthenticationManager(JedisClusterPlus jedisPlus, AuthSource source, Algorithm authTokenAlgorithm) {
        this(jedisPlus, source, null, authTokenAlgorithm);
    }

    public JWTJedisClusterPlusSingleAuthenticationManager(JedisClusterPlus jedisPlus, AuthSource source, AuthUserService authUserService, Algorithm authTokenAlgorithm) {
        super(jedisPlus, source, authUserService);
        this.authTokenAlgorithm = Objects.requireNonNull(authTokenAlgorithm, "JWT 实现的 AuthToken 用于加密解密的工具 Algorithm 实例不能为 null");
    }


    @Override
    protected Logger protected_getLogger() {
        return LOGGER;
    }


    @Override
    public AuthToken decryptAuthToken(String encryptedToken) {
        return new JWTAuthToken(this.authTokenAlgorithm, encryptedToken);
    }

    @Override
    public AuthToken newEmptyAuthToken() {
        return new JWTAuthToken(this.authTokenAlgorithm);
    }

    public Algorithm getAuthTokenAlgorithm() {
        return this.authTokenAlgorithm;
    }
}
